package com.tony.netty.client;

import com.tony.netty.PrintChannel;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author xianghao
 * @date 2018/3/28 9:41
 */
public class ClientMain {

    public static void main(String[] args) {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(new NioEventLoopGroup(1))
                .channel(NioSocketChannel.class)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS,50000)
                .option(ChannelOption.SO_KEEPALIVE,true)
                .option(ChannelOption.TCP_NODELAY,true)
                .handler(new ChannelInboundHandlerAdapter(){
                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        System.out.println("client-channelRead:");
                        PrintChannel.print(ctx.channel());
                        super.channelRead(ctx, msg);
                    }

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("client-channelActive:");
                        PrintChannel.print(ctx.channel());
                        super.channelActive(ctx);
                    }

                    @Override
                    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("client-channelUnregistered:");
                        PrintChannel.print(ctx.channel());
//                        super.channelUnregistered(ctx);
                        ctx.channel().eventLoop().shutdownGracefully();
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("client-channelInactive:");
                        PrintChannel.print(ctx.channel());
                        super.channelInactive(ctx);
                    }

                    @Override
                    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("client-channelRegistered:");
                        PrintChannel.print(ctx.channel());
                        super.channelRegistered(ctx);
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                        System.out.println("client-exceptionCaught:");
                        PrintChannel.print(ctx.channel());
                        cause.printStackTrace();
                    }

                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                        System.out.println("client-userEventTriggered:");
                        PrintChannel.print(ctx.channel());
                        System.out.println(evt);
                        super.userEventTriggered(ctx, evt);
                    }
                })
                .connect("127.0.0.1",8800);

    }
}
